package cn.com.headfree.welfare.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.welfare.entity.FlRegistrationdeath;
import cn.com.headfree.welfare.service.FlRegistrationdeathService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 离退休人员离世登记
 * @author gaohao
 * @since 2021-12-20 21:01:06
 */
@RestController
@RequestMapping("flRegistrationdeath")
public class FlRegistrationdeathController extends ExBaseController {
    private static final Logger LOG = LogManager.getLogger(FlRegistrationdeathController.class);
    /**
     * 服务对象
     */
    @Resource
    private FlRegistrationdeathService flRegistrationdeathService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ResultData selectOne(Integer id, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            FlRegistrationdeath flRegistrationdeath=this.flRegistrationdeathService.queryById(id);
            if(flRegistrationdeath!=null)
            {
                result.setSuccess(true);
                result.setData(flRegistrationdeath);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("查询失败");
            }
            
           
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 新增数据
     *
     * @param flRegistrationdeath 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public ResultData insert(@RequestBody FlRegistrationdeath flRegistrationdeath, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            flRegistrationdeath.setOrgId(user.getBaseOrgId());
            flRegistrationdeath.setCreaterId(user.getId());
            flRegistrationdeath.setCreaterName(user.getName());
            flRegistrationdeath.setCreateTime(new Date());
            flRegistrationdeath.setDelSign(0);
            flRegistrationdeath=this.flRegistrationdeathService.insert(flRegistrationdeath);
            if(flRegistrationdeath!=null)
            {
                result.setSuccess(true);
                result.setData(flRegistrationdeath);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("添加失败");
            }


        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param flRegistrationdeath 实例对象
     * @return 实例对象
     */
    @PostMapping("update")
    public ResultData update(@RequestBody FlRegistrationdeath flRegistrationdeath, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            flRegistrationdeath.setOrgId(user.getBaseOrgId());
            flRegistrationdeath.setUpdaterId(user.getId());
            flRegistrationdeath.setUpdaterName(user.getName());
            flRegistrationdeath.setCreateTime(new Date());
            flRegistrationdeath=this.flRegistrationdeathService.update(flRegistrationdeath);
            if(flRegistrationdeath!=null)
            {
                result.setSuccess(true);
                result.setData(flRegistrationdeath);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("修改失败");
            }
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @PostMapping("deleteById")
    public ResultData deleteById(Integer id, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            FlRegistrationdeath flRegistrationdeath=this.flRegistrationdeathService.queryById(id);
            if(flRegistrationdeath==null){
                result.setSuccess(false);
                result.setData("未查询到数据");
                return result;
            }
            flRegistrationdeath.setOrgId(user.getBaseOrgId());
            flRegistrationdeath.setUpdaterId(user.getId());
            flRegistrationdeath.setUpdaterName(user.getName());
            flRegistrationdeath.setCreateTime(new Date());
            flRegistrationdeath.setDelSign(1);
            flRegistrationdeath=this.flRegistrationdeathService.update(flRegistrationdeath);
            if(flRegistrationdeath!=null)
            {
                result.setSuccess(true);
                result.setData(flRegistrationdeath);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("修改失败");
            }
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }


    /**
     * 通过实体作为筛选条件查询
     *
     * @param flRegistrationdeath 实例对象
     * @return 对象列表
     */
    @PostMapping("queryAll")
    public ResultData queryAll( @RequestBody FlRegistrationdeath flRegistrationdeath, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            flRegistrationdeath.setDelSign(0);
            PageInfo<FlRegistrationdeath> list=this.flRegistrationdeathService.queryAll(flRegistrationdeath);
            if(flRegistrationdeath!=null)
            {
                result.setSuccess(true);
                result.setData(list);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("修改失败");
            }
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

}
